package com.lft.com.oilgas.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.lft.com.oilgas.config.aliOSSProperties;
import com.lft.com.oilgas.entity.Admin;
import com.lft.com.oilgas.entity.Merchant;
import com.lft.com.oilgas.service.IMerchantService;
import com.lft.com.oilgas.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyqw
 * @since 2024-09-10
 */

@RestController
@CrossOrigin
@RestControllerAdvice
@RequestMapping("/util/merchant")
public class MerchantController {
    @Autowired
    private IMerchantService iMerchantService;


    //处理异常
    @ExceptionHandler(Exception.class)
    private Result yichang(Exception e) {
        //noinspection CallToPrintStackTrace
        e.printStackTrace();
        return new Result(false, "服务器异常，请稍后重试");
    }

    //查询用户详情
    @RequestMapping("/merchant/{id}")
    public Result Byid(@PathVariable("id") Integer id) {
        Merchant merchant = iMerchantService.selectByid(id);
        return new Result(true, merchant);
    }

    //管理员查询所有用户
    @Transactional
    @RequestMapping("/merchants")
    public Result getAllMerchants(@RequestBody Map map) {
        List<Map<String, Object>> merchants = iMerchantService.getAllMerchantsWithLegalPersonName(map);
        int total = merchants.size();
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("merchants", merchants);
        resultData.put("total", total);
        return new Result(true, resultData);
    }

    //添加或修改
    @Transactional
    @RequestMapping("/insertOrUpdateMerchant")
    public Result insertOrUpdateMerchant(@RequestBody Merchant merchant) {
        if (iMerchantService.insertOrUpdateMerchant(merchant) > 0) {
            return new Result(true, null);
        } else {
            return new Result(false, null);
        }
    }

    //查询账号相关的信息
    @RequestMapping("/one/{id}")
    public Result selectOne(@PathVariable("id") Integer id) {
        Merchant merchant = iMerchantService.selectOne(id);
        return new Result(true, merchant);
    }

    //根据商户id修改资质
    @Transactional
    @RequestMapping("/qitazz")
    public Result updateByidzz(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        String a = (String) map.get("imgs");
        iMerchantService.updateByidzz(id, a);
        return new Result(true, null);
    }
}
